import requests
import json
import time
from selenium import webdriver
from io import BytesIO
from PIL import Image
from tqdm import tqdm

lines = json.load(open('./data_url/url.json', encoding='utf-8'))
urls = [line['sourceUrl'] for line in lines]
urls.extend([
    'https://mp.weixin.qq.com/s/7VMwyLieSS4GjksDgEOKSg',
    'https://mp.weixin.qq.com/s/76wr5pFWNQ1teYwwvfl_Kw',
    'https://mp.weixin.qq.com/s/zQu6r3pgZMyYEH9JSBBquw',
    'https://mp.weixin.qq.com/s/EuJ1H9xJe8OT-FD_pIX9IQ'
])

img_count = 1
for url in tqdm(urls):
    driver = webdriver.Chrome(executable_path='./chromedriver.exe')
    driver.get(url)
    js = '''
        let i = 0;
        let c = 400;
        let pre_scrollTop = 0
        var inter = setInterval(function () {
                i = i + c;
                scrollTo(0, i);
                let cur_scrollTop = document.documentElement.scrollTop;
                
                if (cur_scrollTop - pre_scrollTop < c) {
                    clearInterval(inter);
                }
            }, 1000);
        
        
        
    '''
    driver.execute_script(js)

    time.sleep(60)

    lines = driver.find_elements_by_xpath('//*/img')

    for line in lines:
        try:
            img_url = line.get_attribute('src')
            response = requests.get(img_url, stream=True)

            bytes_stream = BytesIO(response.content)
            roiImg = Image.open(bytes_stream)
            imgByteArr = BytesIO()
            roiImg.save(imgByteArr, format='PNG')
            imgByteArr = imgByteArr.getvalue()

            with open('./wechat_images/{}.png'.format(img_count), 'wb') as f:
                f.write(imgByteArr)

            img_count += 1
        except:
            # print(img_url)
            continue

    driver.quit()
    # raise Exception('ting')


